<?php

class PlatiComanda {
	public $id_user;
	public $id_platicomanda;
	public $tip;
	public $id_comanda;
	public $moneda;
	public $suma;	
	public $data;	
	public $version;
	public $deleted;		//0 - Nesters; 1 - Sters
	
	public function __construct($id_comanda, $id_user, $tip, $moneda, $suma, $data) {
		$this->id_comanda = $id_comanda;
		$this->id_user = $id_user;		
		$this->tip = $tip;				
		$this->moneda = $moneda;		
		$this->suma = $suma;		
		$this->data = $data;
		$this->version = 0;
		$this->deleted = CT_PLATA_NESTERS;
	}
	
	public function queryInsert($db) {
		$query = $db->prepare("INSERT INTO `plati_comanda`  (	`id_comanda` , 			`id_user`, 
																`suma` , 				`moneda` , 
	                        		        					`version` , 			`data` ,
		                        	        					`deleted` ,		        `tip`                	
	                               							) 
		            						VALUES  		(	:id_comanda,    		:id_user, 
		            											:suma,   				:moneda, 
		                      									:version, 				:data, 
		                      									:deleted, 				:tip
	                      									)");
		
		$query->bindParam(':id_comanda', 	($this->id_comanda));
		$query->bindParam(':id_user', 		($this->id_user));
		$query->bindParam(':suma', 			($this->suma));
		$query->bindParam(':moneda', 		($this->moneda));
		$query->bindParam(':version', 		($this->version));
		$query->bindParam(':data', 			($this->data));
		$query->bindParam(':deleted', 		($this->deleted));
		$query->bindParam(':tip', 			($this->tip));

		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
	  		$query->closeCursor();	  		
			throw new QueryInsertPlatiComandaException("Error inserting plata comanda with id=" . $this->id_comanda . $errInfo);
		}
		
  		$this->id_platicomanda = (int) $db->lastInsertId();	  
    
  		$query->closeCursor();	
		return $this->id_platicomanda; 		//return the plati_comanda's id
	}
	
	public static function getPlataStatusForComanda($db, $idComanda, $tipPlata, $idClient) {
		//check for correct ID for commanda corresponds to the idClient
        $query = $db->prepare("SELECT id
                                FROM comanda
                                WHERE 
                                    comanda.id = :id_comanda AND
                                    comanda.id_client = :idClient");
        $query->bindParam(':id_comanda',    $idComanda);
        $query->bindParam(':idClient',      $idClient);        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
            throw new getPlataStatusForComandaInvalidIDException("id client does not match with the id in commanda: " . $idComanda . $errInfo);
        }
        
        $data = $query->fetch(PDO::FETCH_ASSOC);        
        $query->closeCursor();
        
        if (!isset($data['id']))
            throw new getPlataStatusForComandaInvalidIDException("id client does not match with the id in commanda: " . $idComanda . $errInfo);        
        
        $query = $db->prepare("SELECT ROUND(SUM(suma), 2) AS total
								FROM plati_comanda, comanda 
								WHERE 
									plati_comanda.id_comanda = comanda.id
								AND 
									comanda.id_client = :idClient AND
									plati_comanda.id_comanda = :id_comanda AND
									plati_comanda.tip = :tip_plata AND
									plati_comanda.deleted = ".CT_PLATA_NESTERS.";");
		
		$query->bindParam(':id_comanda', 	$idComanda);
		$query->bindParam(':tip_plata', 	$tipPlata);
		$query->bindParam(':idClient', 		$idClient);
	    	    
		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
	  		$query->closeCursor();
			throw new getPlataStatusForComandaException("Error getting status for comanda with id: " . $idComanda . $errInfo);
		}
		
		$data = $query->fetch(PDO::FETCH_ASSOC);		
		$query->closeCursor();
		
		return $data['total'];
	}
	
	public static function getPlataStatusForComandaForEditedPlata($db, $idComanda, $idPlata, $tipPlata) {
		$query = $db->prepare("SELECT SUM(suma) AS total
						        FROM plati_comanda
						        WHERE 
							        id_comanda = :id_comanda AND
							        tip = :tip_plata AND
							        id != :id_plata AND 
							        deleted = 0;");
		
		$query->bindParam(':id_comanda', 	$idComanda);
		$query->bindParam(':tip_plata', 	$tipPlata);
		$query->bindParam(':id_plata', 		$idPlata);
				
		
		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
	  		$query->closeCursor();
			throw new getPlataStatusForComandaException("Error getting status for comanda with id: " . $idComanda . $errInfo);
		}		
		
		$data = $query->fetch(PDO::FETCH_ASSOC);		
		$query->closeCursor();

		return $data['total'];
	}
	
	public function queryUpdate($db) {
		$this->version++;        
		$query = $db->prepare("UPDATE `plati_comanda` 	SET    	`id_comanda` = :id_comanda , 	`moneda` = :moneda,
																`suma` = :suma , 				`version` = :version,
		                        	        					`data` = :data ,				`deleted` = :deleted
           	  					WHERE id = :idPlata
           	  						AND version < :version");
        
		$query->bindParam(':id_comanda', 		($this->id_comanda));
		$query->bindParam(':moneda', 			($this->moneda));
		$query->bindParam(':suma', 				($this->suma));
		$query->bindParam(':version', 			($this->version));
		$query->bindParam(':data', 				($this->data));
		$query->bindParam(':deleted', 			($this->deleted));
		$query->bindParam(':idPlata', 			($this->id_platicomanda));
		
		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
			throw new QueryUpdatePlatiComandaException("Error updating plati comanda for comanda id " . $this->id_comanda . $errInfo);
        }
        
	  	if ($query->rowCount() == 0) {
            throw new QueryWrongVersionException("Failed updating plati comanda. Reason: ---wrong version!" . $this->id_comanda);
        }

      	$query->closeCursor();
	}
	
	public static function queryLoadById($db, $id_platicomanda, $idClient) {
		$query = $db->prepare("SELECT 
                                    plati_comanda.id_comanda,
                                    plati_comanda.id_user,
                                    plati_comanda.tip,
                                    plati_comanda.moneda,
                                    plati_comanda.suma,
                                    plati_comanda.data,
                                    plati_comanda.version,
                                    plati_comanda.id,
                                    plati_comanda.deleted
                    			FROM plati_comanda, comanda
                                WHERE 
									plati_comanda.id_comanda = comanda.id
                    			AND plati_comanda.id = :id_platicomanda 
                                    AND comanda.id_client = :id_client");
		
        $query->bindParam(':id_platicomanda', $id_platicomanda);
		$query->bindParam(':id_client', $idClient);
		
		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
			throw new QueryLoadPlatiComandaException("Failed loading plati comanda with id: " . $id_platicomanda . $errInfo);
		}
		
		$data = $query->fetch(PDO::FETCH_ASSOC);		
        if (!isset($data['id'])) {
        	$query->closeCursor();
        	throw new QueryLoadPlatiComandaException("Failed loading plati comanda with id: " . $id_platicomanda . " reason: id not existing");        	                   
        }        

        $plata = new PlatiComanda($data['id_comanda'], $data['id_user'], $data['tip'], $data['moneda'], $data['suma'], $data['data']);
        $plata->version = (int) $data['version'];
        $plata->deleted = $data['deleted'];
        $plata->id_platicomanda = $data['id'];
        $query->closeCursor();
        return $plata;
	}
	
	public static function getComandaID($db, $idPlata, $idClient) {
		//get the id for comanda based on the id of the plata
		$query = $db->prepare("SELECT id_comanda 
								FROM plati_comanda, comanda 
								WHERE plati_comanda.id_comanda = comanda.id
								AND plati_comanda.id = :idPlataComanda
                                AND comanda.id_client = :id_client
								LIMIT 1");		
        $query->bindParam(':idPlataComanda', $idPlata);                
		$query->bindParam(':id_client', $idClient);				
		
		if ($query->execute() === FALSE) {			
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
			$query->closeCursor();
    	  	throw new QueryGetComandaIDException("Failed getting id for commanda based on plata id." . $errInfo);
		}	
				
		$data = $query->fetch(PDO::FETCH_ASSOC);		
		$query->closeCursor();
		//end get the id for comanda based on the id of the plata
		return $data['id_comanda'];
	}
	
	public function queryLoadByComanda($db, $id_comanda, $id_client) {
		$query = $db->prepare("SELECT *
                    			FROM plati_comanda pc, comanda c
                    			WHERE pc.id_comanda = $id_comanda
                     			AND  c.id_client = :id_client");
		
		$query->bindParam(':id_client', $id_client);
		                     			
		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
			throw new QueryLoadPlatiComandaException("Failed loading plati comanda with id: " . $id_comanda . $errInfo);            	       
		}
            
        $plati = array();
        
        while ($data = $query->fetch(PDO::FETCH_ASSOC)) {
        	$plata = new PlatiComanda($data['id_comanda'], $data['moneda_plata'], $data['suma_plata'], $data['moneda_incasare'], $data['suma_incasare']);
        	$plata->version = (int) $data['version'];
        	$plata->deleted = $data['deleted'];
        	$plata->id_platicomanda = $data['id_platicomanda'];
        	$plati[] = $plata;
        }
        
        $query->closeCursor();
        return $plati;
	}		
	
	public function queryDelete($db) {
      	$query = $db->prepare("DELETE FROM plati_comanda WHERE id = :id_platicomanda");      	
      	$query->bindParam(':id_platicomanda', $this->id_platicomanda);
      	
      	if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
      		$query->closeCursor();
      		throw new QueryDeletePlatiComandaException("Faild deleting plati comanda with id: " . $this->id_platicomanda . $errInfo);            	       
      	}
      	
      	$query->closeCursor();
    	return CT_RESULT_OK;      	
	}
	
	public static function queryDeleteById($db, $id_platicomanda) {
        //check the id_platicomanda
        $query = $db->prepare("SELECT plati_comanda.id, plati_comanda.id_comanda FROM plati_comanda, comanda 
								WHERE plati_comanda.id_comanda = comanda.id 
								AND plati_comanda.id = :id_platicomanda");
        $query->bindParam(':id_platicomanda', $id_platicomanda);
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
            throw new QueryDeletePlatiComandaException("Failed deleting plati comanda with id: " . $id_platicomanda . $errInfo);                       
        }
                
        //if the id is valid!
        if ($query->rowCount() == 1) {
      	    $query = $db->prepare("UPDATE plati_comanda SET deleted = :deleted
      								    WHERE id = :id_platicomanda");
      	
      	    $query->bindParam(':id_platicomanda', ($id_platicomanda));
      	    $query->bindValue(':deleted', CT_DELETED);
      	
      	    if ($query->execute() === FALSE) {
                $errInfo = $query->errorInfo();
                $errInfo = $errInfo[2];
      		    $query->closeCursor();
      		    throw new QueryDeletePlatiComandaException("Failed deleting plati comanda with id: " . $id_platicomanda . $errInfo);            	       
      	    }
        }
      	
      	$query->closeCursor();    	
	}
	
	public static function queryDeleteByComanda($db, $id_comanda) {
      	$query = $db->prepare("DELETE FROM plati_comanda WHERE id_comanda = :id_comanda");
      	$query->bindParam(':id_comanda', $id_comanda);
      	
      	if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
      		$query->closeCursor();
      		throw new QueryDeletePlatiComandaException("Failed deleting plati comanda with id: " . $id_comanda . $errInfo);            	       
      	}
      	
      	$query->closeCursor();
    	return CT_RESULT_OK;      	
	}
}

?>